import os
import sys
from PyQt6.QtWidgets import QLabel
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QDragEnterEvent, QDropEvent

# Add parent directory to path to allow absolute imports
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

class DropArea(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.update_style()

    def update_style(self, is_dark=False):
        style = '''
            QLabel {
                border: 2px dashed #aaa;
                border-radius: 10px;
                padding: 40px;
                color: #666;
                font-size: 16px;
                margin: 10px 0;
            }
            QLabel:hover {
                border-color: #4CAF50;
                background-color: #f0f8f0;
            }
        '''
        if is_dark:
            style = style.replace('#666', '#aaa').replace('#f0f8f0', '#1e3a1e')
        self.setStyleSheet(style)
        self.setText('Drag & Drop PDF file here\nor click to browse')

    def dragEnterEvent(self, event: QDragEnterEvent):
        if event.mimeData().hasUrls():
            urls = event.mimeData().urls()
            if urls and urls[0].toLocalFile().lower().endswith('.pdf'):
                event.acceptProposedAction()
                return
        event.ignore()

    def dropEvent(self, event: QDropEvent):
        if event.mimeData().hasUrls():
            file_path = event.mimeData().urls()[0].toLocalFile()
            if file_path.lower().endswith('.pdf'):
                self.parent().set_pdf_path(file_path)